//
//  GoodsViewController.m
//  Leisure
//
//  Created by xalo on 16/6/30.
//  Copyright © 2016年 朱旭东. All rights reserved.
//

#import "GoodsViewController.h"
#import "GoodsCell.h"

@interface GoodsViewController ()<UITableViewDataSource,UITableViewDelegate>

//@property(nonatomic,strong)GoodsModel *model;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSourceArray;
@property(nonatomic,strong)NSString *string;

@end

@implementation GoodsViewController

 static NSNumber const  *indexpath ;

- (NSMutableArray *)dataSourceArray
{

    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array] ;
    }
    return _dataSourceArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor  greenColor];
    
    [RequestManager fetchDataWithURL:kGoodsurl parameters:@{} method:@"POST" complet:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *subDic in dic[@"data"][@"list"]) {
            GoodsModel *model = [[GoodsModel alloc] init];
            [model  setValuesForKeysWithDictionary:subDic];
            [self.dataSourceArray addObject:model];
        }
    } updataUI:^{
        [self.tableView reloadData];
    }];
    
    [self.tableView  registerNib:[UINib nibWithNibName:@"GoodsCell" bundle:nil] forCellReuseIdentifier:@"Goods_cell"];
}


#pragma mark ------------------TableViewDelegate------------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    GoodsCell *cell =[tableView dequeueReusableCellWithIdentifier:@"Goods_cell" forIndexPath:indexPath];
    
    indexpath = [NSNumber numberWithInteger:indexPath.row];
    GoodsModel *model =[[GoodsModel alloc] init];
    model = self.dataSourceArray[indexPath.row];
    self.string = model.buyurl;
    
    cell.btnBlock = ^(){
         BuyViewController *buyVC = [[BuyViewController alloc] init];
        buyVC.buyUrl =[self.dataSourceArray[indexPath.row] buyurl] ;
        [self.navigationController pushViewController:buyVC animated:YES];
        //直接在网页上打开
//         [[UIApplication sharedApplication]openURL:[NSURL URLWithString:self.str]];
    };
    
    [cell setCellWIthModel:model];

    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}



//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    BuyViewController *buyVC = [[BuyViewController alloc] init];
//    buyVC.byUrl =[self.dataSourceArray[indexPath.row] buyurl] ;
//    [self.navigationController pushViewController:buyVC animated:YES];
//   
//}





@end
